var count = 0
var state = {
  isReFlash: false,
  ping: 300
}
var bigTimes = 100
function beginFlash (cd) {
  bigTimes = 10 * 60 * 1000 / state.ping
  console.log('bigTimes', bigTimes, cd, chrome)
  // active: true, currentWindow: true
  chrome.tabs.query({ active: true, currentWindow: true}, function (tab) { // 获取当前tab
    // 向tab发送请求
    console.log('tab', tab)
    chrome.tabs.sendMessage(tab[0].id, {
      action: 'begin',
      ping: state.ping
    }, function (response) {
      if (response && response.state == 'ok') {
        if (response.isStop) {
          state.isReFlash = false
        }
        count++
        cd && cd()
        if (count > bigTimes) {
          state.isReFlash = false
          count = 0
        }
      }
    })
    if (state.isReFlash) {
      setTimeout(function () {
        beginFlash(cd)
      }, state.ping)
    }
  })
}
chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
    if (request.action == 'onload') {
      console.log('onload', request)
    }
  }
)
chrome.contextMenus.create({
  title: '测试右键菜单',
  onclick: function () { alert('您点击了右键菜单！') }
})
